package com.caipin.utils;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-10-06 12:35
 * @Description: TODO
 */
public class HttpClientUtils {
    private static final Logger log = LoggerFactory.getLogger(com.caipin.utils.HttpClientUtils.class);

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static Map<String, Object> doGet(String url) {
        Map<String, Object> map = new HashMap<>();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);
        try {
            CloseableHttpResponse closeableHttpResponse = httpClient.execute((HttpUriRequest)httpGet);
            if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
                String jsonResult = EntityUtils.toString(closeableHttpResponse.getEntity());
                map = (Map<String, Object>)objectMapper.readValue(jsonResult, Map.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static String doPost(String url, String data, int timeout, Map<String, String> headers) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setRedirectsEnabled(true).build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        httpPost.addHeader("Content-Type", "application/json");
        for (Map.Entry<String, String> entry : headers.entrySet())
            httpPost.addHeader(entry.getKey(), entry.getValue());
        if (data != null && data instanceof String) {
            StringEntity stringEntity = new StringEntity(data, "UTF-8");
            httpPost.setEntity((HttpEntity)stringEntity);
        }
        try {
            CloseableHttpResponse httpResponse = httpClient.execute((HttpUriRequest)httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(httpEntity);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String doPut(String url, String data, int timeout) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setRedirectsEnabled(true).build();
        HttpPut httpPut = new HttpPut(url);
        httpPut.setConfig(requestConfig);
        httpPut.addHeader("Content-Type", "text/html; chartset=UTF-8");
        if (data != null && data instanceof String) {
            StringEntity stringEntity = new StringEntity(data, "UTF-8");
            httpPut.setEntity((HttpEntity)stringEntity);
        }
        try {
            CloseableHttpResponse httpResponse = httpClient.execute((HttpUriRequest)httpPut);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(httpEntity);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
